local hx__fangzhu = fk.CreateSkill({
  name = "hx__fangzhu",
})

hx__fangzhu:addEffect("active", {
  anim_type = "control",
  prompt = "#hx__fangzhu",
  card_num = 0,
  target_num = 1,
  interaction = function(self, player)
    local choiceList = {
      "hx__fangzhu_only_basic",
      "hx__fangzhu_only_trick",
      "hx__fangzhu_only_equip",
      "hx__fangzhu_nullify_skill",
      "hx__fangzhu_disresponsable",
      "hx__fangzhu_turn_over",
    }
    local choices = {}
    for i = 1, math.min(player:getMark("@hx__xingshang_song"), 3) do
      if i == 1 then
        table.insert(choices, "hx__fangzhu_only_basic")
      elseif i == 2 then
        table.insert(choices, "hx__fangzhu_only_trick")
        table.insert(choices, "hx__fangzhu_nullify_skill")
        table.insert(choices, "hx__fangzhu_disresponsable")
      else
        table.insertTable(choices, { "hx__fangzhu_only_equip", "hx__fangzhu_turn_over" })
      end
    end
    return UI.ComboBox { choices = choices, all_choices = choiceList }
  end,
  can_use = function(self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) == 0 and
      player:getMark("@hx__xingshang_song") > 0 and
      player:hasSkill("hx__xingshang", true)
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]

    local choice = self.interaction.data
    if choice:startsWith("hx__fangzhu_only") then
      choice = choice:sub(-5)
      room:removePlayerMark(player, "@hx__xingshang_song", choice == "basic" and 1 or (choice == "trick" and 2 or 3))
      local limit_mark = target:getTableMark("@hx__fangzhu_limit")
      table.insertIfNeed(limit_mark, choice.."_char")
      room:setPlayerMark(target, "@hx__fangzhu_limit", limit_mark)
    elseif choice == "hx__fangzhu_nullify_skill" then
      room:removePlayerMark(player, "@hx__xingshang_song", 2)
      room:setPlayerMark(target, "@@hx__fangzhu_skill_nullified", 1)
    elseif choice == "hx__fangzhu_disresponsable" then
      room:removePlayerMark(player, "@hx__xingshang_song", 2)
      room:setPlayerMark(target, "@@hx__fangzhu_disresponsable", 1)
    elseif choice == "hx__fangzhu_turn_over" then
      room:removePlayerMark(player, "@hx__xingshang_song", 3)
      target:turnOver()
    end
  end,
})

hx__fangzhu:addEffect(fk.TurnEnd, {
  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, markName in ipairs({ "@hx__fangzhu_limit", "@@hx__fangzhu_skill_nullified", "@@hx__fangzhu_disresponsable" }) do
      room:setPlayerMark(player, markName, 0)
    end
  end,
})

hx__fangzhu:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return target == player and table.find(player.room.alive_players, function(p)
      return p:getMark("@@hx__fangzhu_disresponsable") > 0 and p ~= target
    end)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    data.disresponsiveList = data.disresponsiveList or {}
      local tos = table.filter(room.alive_players, function(p)
        return p:getMark("@@hx__fangzhu_disresponsable") > 0 and p ~= target
      end)
      table.insertTableIfNeed(data.disresponsiveList, tos)
  end,
})

hx__fangzhu:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    local typeLimited = player:getTableMark("@hx__fangzhu_limit")
    for _, type in ipairs(typeLimited) do
      if type ~= card:getTypeString() .. "_char" then
        local subcards = Card:getIdList(card)
        return
          #subcards > 0 and
          table.every(subcards, function(id)
            return table.contains(player:getCardIds("h"), id)
          end)
      end
    end
  end,
})

hx__fangzhu:addEffect("invalidity", {
  invalidity_func = function(self, from, skill)
    return from:getMark("@@hx__fangzhu_skill_nullified") > 0 and skill:isPlayerSkill(from)
  end
})

return hx__fangzhu
